package pruebas;

import java.io.IOException;
import java.io.PrintWriter;


import java.net.Socket;

import java.util.Random;

import org.json.JSONException;
import org.json.JSONObject;

import base64.Base64;

public class PaquetePruebaAlMinga {

	public static void main (String[]arg) throws IOException
	{

		byte numSeq = 13;
		byte  numSeqEnc = 0;
		
		byte ruido;
		Random r = new Random();
		r.setSeed(57);
		
		
		for (int i = 0; i < numSeq - 1; i++)
		{
			r.nextLong();
		}
		
		long h = r.nextLong();
		
		ruido = (byte) ( h);

		
		
		
		// comentar uno de los dos
		String cadena = "soy una cadena encriptada";
	
		JSONObject flightdata = new JSONObject();
		JSONObject container = new JSONObject();
		try {

			int[] pos = {1,0,0};
			flightdata.put("position", pos);
			flightdata.put("flightId", 100);
			flightdata.put("source", "Radar");
			flightdata.put("speed", 1);
			flightdata.put("cancelled", false);
			flightdata.put("wait", 0);

			container.put("FlightData", flightdata);
			cadena = container.toString();
			
		} catch (JSONException e) {
		}
		
		
		
		
		
	
	
		
		
		byte[] p = cadena.getBytes();
		
		
		for (int i = 0; i < p.length; i++)
		{
			p[i] = (byte) (p[i] ^ ruido); 
		}


		String cad2 = Base64.encode(p);
		
		numSeqEnc = (byte) (numSeq ^ ruido);
		
		JSONObject data = new JSONObject();

		try {
			data.put("numSeq", numSeq);
			data.put("numSeqEnc", numSeqEnc);
			data.put("datos", cad2);



		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	
		// manda al minga autenticator  por TCP
		String host = "127.0.0.1";
		int port = 10001;
		Socket clientSocket = new Socket(host, port);
		System.out.println("conexion establecida por TCP con minga autenticator");
		PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(),true);
		outToServer.println(data.toString());
		System.out.println("paquete enviado a minga autenticator");
		clientSocket.close();
			
				
	}

}
